//
//  IdCardConfirmInfoView.swift
//  身份证上传 - 确认信息
//
//  Created by 千策万划-mini on 2025/5/3.
//

import Foundation

class IdCardConfirmInfoView: UIView, UITextFieldDelegate {
    
    var blockConfirmClick:(() -> Void)?
    var blockBirthClick:(() -> Void)?
    
    // MARK: - lazy
    lazy var imageBack: UIImageView = {
        let result = UIImageView()
        result.isUserInteractionEnabled = true
        result.widthHeight = XY(W(355), W(508))
        result.image = UIImage(named: "icon_idcard_example_bg")
        return result
    }()
    lazy var btnClose: UIButton = {
        let result = UIButton()
        result.widthHeight = XY(40+16*2, 40+30*2)
        result.tag = 1
        result.addTarget(self, action: #selector(btnClick(_:)), for: .touchUpInside)
        return result
    }()
    lazy var labelTitle: OutlineLabel = {
        let result = OutlineLabel()
        result.resetView(
            title: "Identity",
            textColor: COLOR_WHITE(),
            font: UIFont.customBoldFont(size: 24),
            shadowColor: UIColor(hex: "#4105A0"),
            outlineColor: UIColor(hex: "#3B0B9A")
        )
        return result
    }()
    lazy var labelSubTitle: OutlineLabel = {
        let result = OutlineLabel()
        result.resetView(
            title: "Information",
            textColor: COLOR_WHITE(),
            font: UIFont.customBoldFont(size: 24),
            shadowColor: UIColor(hex: "#4105A0"),
            outlineColor: UIColor(hex: "#3B0B9A")
        )
        return result
    }()
    lazy var imageContent: UIImageView = {
        let result = UIImageView()
        result.isUserInteractionEnabled = true
        result.widthHeight = XY(W(275), W(312))
        result.image = UIImage(named: "icon_idcard_confirm_info_bg")
        return result
    }()
    
    // 名字
    lazy var labelNameTitle: UILabel = {
        let result = UILabel()
        result.textColor = COLOR_LABEL()
        result.font = UIFont.customSemiBoldFont(size: 16)
        result.fitTitle("Full Name", variable: 0)
        return result
    }()
    lazy var imageNameBg: UIButton = {
        let result = UIButton()
        result.isUserInteractionEnabled = true
        result.widthHeight = XY(W(251), W(50))
        result.setBackgroundImage(UIImage(named: "icon_login_agree_bg"), for: .normal)
        result.tag = 4
        result.addTarget(self, action: #selector(btnClick(_:)), for: .touchUpInside)
        return result
    }()
    lazy var tfName: UITextField = {
        let result = UITextField()
        result.font = UIFont.customBoldFont(size: 14)
        result.textColor = COLOR_WHITE()
        result.textAlignment = .left
        result.placeholder = "Full Name"
        result.isHidden = true
        result.delegate = self
        return result
    }()
    lazy var labelName: OutlineLabel = {
        let result = OutlineLabel()
        return result
    }()
    
    // 身份证号码
    lazy var labelNumberTitle: UILabel = {
        let result = UILabel()
        result.textColor = COLOR_LABEL()
        result.font = UIFont.customSemiBoldFont(size: 16)
        result.fitTitle("ID NO.", variable: 0)
        return result
    }()
    lazy var imageNumberBg: UIButton = {
        let result = UIButton()
        result.isUserInteractionEnabled = true
        result.widthHeight = XY(W(251), W(50))
        result.setBackgroundImage(UIImage(named: "icon_login_agree_bg"), for: .normal)
        result.tag = 5
        result.addTarget(self, action: #selector(btnClick(_:)), for: .touchUpInside)
        return result
    }()
    lazy var tfNumber: UITextField = {
        let result = UITextField()
        result.font = UIFont.customBoldFont(size: 14)
        result.textColor = COLOR_WHITE()
        result.textAlignment = .left
        result.placeholder = "ID NO."
        result.isHidden = true
        result.delegate = self
        return result
    }()
    lazy var labelNumber: OutlineLabel = {
        let result = OutlineLabel()
        return result
    }()
    // 日期
    lazy var labelTimeTitle: UILabel = {
        let result = UILabel()
        result.textColor = COLOR_LABEL()
        result.font = UIFont.customSemiBoldFont(size: 16)
        result.fitTitle("Date of Birth", variable: 0)
        return result
    }()
    lazy var imageTimeBg: UIButton = {
        let result = UIButton()
        result.widthHeight = XY(W(251), W(50))
        result.setBackgroundImage(UIImage(named: "icon_login_agree_bg"), for: .normal)
        result.tag = 3
        result.addTarget(self, action: #selector(btnClick(_:)), for: .touchUpInside)
        return result
    }()
    lazy var labelBirth: OutlineLabel = {
        let result = OutlineLabel()
        return result
    }()
    lazy var imageArrow: UIImageView = {
        let result = UIImageView()
        result.widthHeight = XY(W(22), W(22))
        result.image = UIImage(named: "icon_arrow_detail")
        return result
    }()
    lazy var btnConfirm: UIButton = {
        let result = UIButton()
        result.widthHeight = XY(W(168), W(46))
        result.setBackgroundImage(UIImage(named: "icon_pop_cancel"), for: .normal)
        result.tag = 2
        result.addTarget(self, action: #selector(btnClick(_:)), for: .touchUpInside)
        let label = OutlineLabel()
        label.resetView(
            title: "Confirming",
            textColor: COLOR_WHITE(),
            font: UIFont.customBoldFont(size: 16),
            shadowColor: UIColor(hex: "#1B4506"),
            outlineColor: UIColor(hex: "#004600")
        )
        label.centerXCenterY = XY(result.width / 2, result.height / 2)
        result.addSubview(label)
        return result
    }()
    
    // MARK: - init
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.widthHeight = XY(SCREEN_WIDTH, SCREEN_HEIGHT)
        self.backgroundColor = UIColor(hex: "000000", alpha: 0.8)
        
        self.addSubView()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    // MARK: - 添加
    func addSubView() {
        addSubview(imageBack)
        imageBack.addSubview(labelTitle)
        imageBack.addSubview(labelSubTitle)
        imageBack.addSubview(imageContent)
        imageBack.addSubview(btnClose)
        
        imageContent.addSubview(labelNameTitle)
        imageContent.addSubview(imageNameBg)
        imageNameBg.addSubview(labelName)
        imageNameBg.addSubview(tfName)
        
        imageContent.addSubview(labelNumberTitle)
        imageContent.addSubview(imageNumberBg)
        imageNumberBg.addSubview(labelNumber)
        imageNumberBg.addSubview(tfNumber)
        
        imageContent.addSubview(labelTimeTitle)
        imageContent.addSubview(imageTimeBg)
        imageTimeBg.addSubview(labelBirth)
        imageTimeBg.addSubview(imageArrow)
        
        imageBack.addSubview(btnConfirm)
        resetView()
    }
    
    // MARK: 点击事件
    @objc func btnClick(_ sender: UIControl) {
        feedback()
        sender.addClickAnimate()
        switch sender.tag {
        case 1:
            hide()
            break
        case 2:
            if let block = blockConfirmClick {
                block()
            }
            break
        case 3: // 选择日期
            if let block = blockBirthClick {
                block()
            }
            break
        case 4: // 姓名输入框选中
            tfName.isHidden = false
            labelName.isHidden = true
            tfName.becomeFirstResponder()
            break
        case 5: // 身份证输入框选中
            tfNumber.isHidden = false
            labelNumber.isHidden = true
            tfNumber.becomeFirstResponder()
            break
        default:
            break
        }
    }

    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        showName()
        showNumber()
        return true
    }

    // 实现 textFieldDidEndEditing 方法，监听失去焦点事件
    func textFieldDidEndEditing(_ textField: UITextField) {
        if textField == tfName {
            showName()
        }
        if textField == tfNumber {
            showNumber()
        }
    }
    
    func showName() {
        labelName.resetView(
            title: tfName.text ?? "",
            textColor: COLOR_WHITE(),
            font: UIFont.customBoldFont(size: 14),
            shadowColor: UIColor(hex: "#2D0084"),
            outlineColor: UIColor(hex: "#2D0084")
        )
        labelName.leftCenterY = XY(W(10), imageNameBg.height/2)
        tfName.isHidden = isStr(tfName.text ?? "")
        labelName.isHidden = !tfName.isHidden
    }
    
    func showNumber() {
        labelNumber.resetView(
            title: tfNumber.text ?? "",
            textColor: COLOR_WHITE(),
            font: UIFont.customBoldFont(size: 14),
            shadowColor: UIColor(hex: "#2D0084"),
            outlineColor: UIColor(hex: "#2D0084")
        )
        labelNumber.leftCenterY = XY(W(10), imageNumberBg.height/2)
        tfNumber.isHidden = isStr(tfNumber.text ?? "")
        labelNumber.isHidden = !tfNumber.isHidden
    }
    
    // 显示
    func show() {
        if let window = WindowManager.shared.getCurrentWindow() {
            window.endEditing(true)
            // 设置初始状态（比如从底部开始）
            alpha = 0
            imageBack.transform = CGAffineTransform(translationX: 0, y: 50)
            window.addSubview(self)
            // 添加弹出动画
            UIView.animate(withDuration: 0.3, delay: 0, options: .curveEaseOut) {
                self.alpha = 1
                self.imageBack.transform = .identity
            }
        }
    }

    // 隐藏
    func hide() {
        // 添加消失动画
        UIView.animate(withDuration: 0.3, delay: 0, options: .curveEaseIn) {
            self.alpha = 0
            self.imageBack.transform = CGAffineTransform(translationX: 0, y: 50)
        } completion: { _ in
            self.removeFromSuperview()
            // 重置 transform，以便下次显示
            self.imageBack.transform = .identity
        }
    }
    
    // MARK: - 重置界面
    func resetView(name: String = "", number: String = "", birth: String = "") {
        imageBack.centerXCenterY = XY(SCREEN_WIDTH/2, SCREEN_HEIGHT/2)
        btnClose.rightTop = XY(imageBack.width, 0)
        
        labelTitle.centerXTop = XY(imageBack.width/2, W(16))
        labelSubTitle.centerXTop = XY(imageBack.width/2, labelTitle.bottom-W(17))
        
        imageContent.centerXTop = XY(imageBack.width/2, W(112))
        
        // 姓名
        let heightMark = W(47)
        labelNameTitle.leftCenterY = XY(W(12), heightMark/2+W(6))
        imageNameBg.centerXTop = XY(imageContent.width/2, labelNameTitle.bottom+W(10))
        tfName.widthHeight = XY(imageNameBg.width-W(10)*2, imageNameBg.height-20)
        tfName.leftCenterY = XY(W(10), imageNameBg.height/2)
        tfName.text = name
           
        labelName.isHidden = !isStr(name)
        tfName.isHidden = isStr(name)
        if isStr(name) {
            labelName.resetView(
                title: name,
                textColor: COLOR_WHITE(),
                font: UIFont.customBoldFont(size: 14),
                shadowColor: UIColor(hex: "#2D0084"),
                outlineColor: UIColor(hex: "#2D0084")
            )
            labelName.leftCenterY = XY(W(10), imageNameBg.height/2)
        }
        
        // 身份证好嘛
        labelNumberTitle.leftCenterY = XY(labelNameTitle.left, imageNameBg.bottom+heightMark/2)
        imageNumberBg.centerXTop = XY(imageContent.width/2, labelNumberTitle.bottom+W(10))
        
        tfNumber.widthHeight = XY(imageNumberBg.width-W(10)*2, imageNumberBg.height-20)
        tfNumber.leftCenterY = XY(W(10), imageNumberBg.height/2)
        tfNumber.text = number
           
        labelNumber.isHidden = !isStr(number)
        tfNumber.isHidden = isStr(number)
        if isStr(number) {
            labelNumber.resetView(
                title: number,
                textColor: COLOR_WHITE(),
                font: UIFont.customBoldFont(size: 14),
                shadowColor: UIColor(hex: "#2D0084"),
                outlineColor: UIColor(hex: "#2D0084")
            )
            labelNumber.leftCenterY = XY(W(10), imageNameBg.height/2)
        }
        
        // 生日
        labelTimeTitle.leftCenterY = XY(labelNameTitle.left, imageNumberBg.bottom+heightMark/2)
        imageTimeBg.centerXTop = XY(imageContent.width/2, labelTimeTitle.bottom+W(10))
        
        resetBirth(birth: birth)
        imageArrow.rightCenterY = XY(imageTimeBg.width-W(10), imageTimeBg.height/2)
        
        btnConfirm.centerXTop = XY(imageBack.width/2, imageContent.bottom+W(15))
    }
    
    // 重置生日
    func resetBirth(birth: String = "") {
        labelBirth.resetView(
            title: UnPackStr(birth),
            textColor: COLOR_WHITE(),
            font: UIFont.customBoldFont(size: 14),
            shadowColor: UIColor(hex: "#2D0084"),
            outlineColor: UIColor(hex: "#2D0084")
        )
        labelBirth.leftCenterY = XY(W(10), imageTimeBg.height/2)
    }
    
}
